package com.sczmgk.hotline_pzh.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.text.InputFilter;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseDialog;

/**
 * 消息提示框
 *
 * @author
 */
@SuppressLint("ResourceAsColor")
public class MessageDialog extends BaseDialog {

    private boolean canNotCancel;

    private TextView tvMessage;

    private View contentView;

    private EditText editText;
    private TextView woman;
    private TextView man;
    private final int WOMAN = 0;
    private final int MAN = 1;


    /**
     * @param context
     */
    public MessageDialog(Context context) {
        super(context);
        contentView = LayoutInflater.from(getContext()).inflate(
                R.layout.common_message_dialog, null);
        tvMessage = (TextView) contentView.findViewById(R.id.tv_message);
        editText = (EditText) contentView.findViewById(R.id.edittext);
    }

    /**
     * @param context
     */
    public MessageDialog(Context context , View view) {
        super(context);
        contentView = view;
    }

    /**
     * @param context
     * @param canNotCancel
     */
    public MessageDialog(Context context, boolean canNotCancel) {
        super(context);
        contentView = LayoutInflater.from(getContext()).inflate(
                R.layout.common_message_dialog, null);
        tvMessage = (TextView) contentView.findViewById(R.id.tv_message);
        editText = (EditText) contentView.findViewById(R.id.edittext);
        this.canNotCancel = canNotCancel;
    }


    /**
     * 设置按钮上的线是否显示
     *
     * @param visibility
     */
    public void setBtnTopLineVisibility(int visibility) {
        super.setBtnTopLineVisibility(visibility);
    }

    /**
     * @param context
     * @param layout
     */
    public MessageDialog(final Context context, int layout, final OnSexChangedListener saxChangedListener, String sex) {
        super(context);

        contentView = LayoutInflater.from(getContext()).inflate(layout, null);

        woman = (TextView) contentView.findViewById(R.id.tv_woman);
        man = (TextView) contentView.findViewById(R.id.tv_man);
        setSex(sex);
        woman.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                saxChangedListener.onSaxChanged(MAN);
                dismiss();
            }
        });
        man.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                saxChangedListener.onSaxChanged(WOMAN);
                dismiss();
            }
        });

    }

    /**
     * 性别变化的监听接口
     *
     * @author bo
     */
    public interface OnSexChangedListener {
        void onSaxChanged(int sax);
    }

    @Override
    public View createContentView() {
        return contentView;
    }

    public void setSex(String sex) {
        switch (Integer.valueOf(sex)) {
            case MAN:
                woman.setPressed(true);
//			woman.setBackgroundColor(context.getResources().getColor(R.color.common_grey_bg));
                break;
            case WOMAN:
                man.setPressed(true);
//			man.setBackgroundColor(context.getResources().getColor(R.color.common_grey_bg));
                break;

            default:
                break;
        }
    }

    /**
     * 设置对话框信息
     *
     * @param text
     */
    public void setMessage(String text) {
        tvMessage.setText(text);
    }

    /**
     * 设置对话框信息
     *
     * @param text   文字内容
     * @param isHtml 是否为html
     */
    public void setMessage(String text, boolean isHtml) {
        if (isHtml) {
            tvMessage.setText(Html.fromHtml(text));
        } else {
            tvMessage.setText(text);
        }
    }

    /**
     * 设置是否居中
     *
     * @param gravity
     */
    public void setTextViewGravity(int gravity) {
        tvMessage.setGravity(gravity);
    }

    /**
     * 设置是否能关闭
     *
     * @param canNotCancel
     */
    public void setCanNotCancel(boolean canNotCancel) {
        this.canNotCancel = canNotCancel;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                || keyCode == KeyEvent.KEYCODE_SEARCH) {
            if (canNotCancel) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置一个按钮可见
     */
    public void setOneBtnStyle() {
        setBtn1Visible(false);
        setBtn2Visible(false);
        setBtn3Visible(false);
        setBtn4Visible(true);
    }

    /**
     * 不显示按钮
     */
    public void setBtnContentGone() {
        setBtnConetntGone();
    }

    /**
     * 设置两个按钮可见
     */
    public void setTwoBtnStyle() {
        setBtn1Visible(true);
        setBtn2Visible(true);
        setBtn3Visible(false);
        setBtn4Visible(false);
    }

    /**
     * 设置三个按钮可见
     */
    public void setThreeBtnStyle() {
        setBtn1Visible(true);
        setBtn2Visible(true);
        setBtn3Visible(true);
        setBtn4Visible(false);
    }

    /**
     * 设置一个按钮的点击监听事件
     *
     * @param onClickListener
     */
    public void setOneBtnClickListener(
            View.OnClickListener onClickListener) {
        setBtn4ClickListener(onClickListener);
    }

    /**
     * 设置EditText是否显示
     *
     * @param visble
     */
    public void setEditTextVisible(boolean visble) {
        if (visble) {
            editText.setVisibility(View.VISIBLE);
        } else {
            editText.setVisibility(View.GONE);
        }
    }

    /**
     * 设置是否是编辑密码
     *
     * @param is
     */
    public void setEditPassword(boolean is) {
        if (is) {
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }

    /**
     * 取得EditText的文字
     *
     * @return
     */
    public String geteditText() {
        return editText.getText().toString();
    }

    /**
     * 设置Title
     *
     * @param title 文字
     */
    public void setTitle(String title) {
        super.setTitle(title);
    }

    /**
     * 设置title颜色
     */
    public void setTitleTextColor(int ResId) {
        super.setTitleTextColor(ResId);
    }

    /**
     * 设置Title
     *
     * @param rsid title 的id
     */
    public void setTitle(int rsid) {
        super.setTitle(rsid);
    }

    /**
     * 设置EditText的文字长度
     *
     * @param length
     */
    public void setEditTextLength(int length) {
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
    }

    /**
     * 取得editText控件
     *
     * @return
     */
    public EditText getEditText() {
        return editText;
    }

    /**
     * 设置title 文字位置
     */
    public void setTitleGravity(int gravity) {
        super.setTitleGravity(gravity);
    }
}
